import { AnalysisInfo } from '@/types/AnalysisInfo';
import service from '@/utils/http';
import { ApiBase } from '@/types/api/apiBase';
import currencylist from '@/models/currencylist';
import formatHash from '@/utils/format/formatHash';

export function fetchAnalysisInfo() {
  return new Promise<AnalysisInfo>(async (resolve, reject) => {
    const res = await service.get<ApiBase<AnalysisInfo>>('/web/info');
    const currencyMap = await currencylist.getCurrencyMap();
    if (res && res.data && res.data.code === 0) {
      const result = res.data.result;
      if (result.coin) {
        const legendData = (result.legendData = Object.keys(result.coin));
        result.seriesData = legendData.map((l) => {
          const value = result.coin[l];
          return {
            value,
            name: l,
            hashFormat: formatHash(value, l, currencyMap.get(l)),
          };
        });
      }
      resolve(result);
    } else {
      reject();
    }
  });
}
